﻿<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
		<meta name="apple-mobile-web-app-title" content="思路广惠">
		<title>添加地址</title>

		<meta content="telephone=no" name="format-detection" />

		<link rel="stylesheet" type="text/css" href="../../src/css/base.css?1.0" />
	</head>

	<body class="body-bg">
		<header class="header">
			<a href='javascript: history.back(-3);' class="head-le"><i class="iconfont icon-fanhui i2m"></i></a>
			<h1 class="header-txt">编辑地址</h1>
			<div class="head-ri" id="saves">保存</div>
		</header>
		<section class="top1rem">
			<ul class="setup-u">
				<li class="address-inp setup-btm">
					<span> 收货人 </span> <input type="text" class="addRess" />
				</li>
				<li class="address-inp setup-btm">
					<span>联系电话 </span> <input type="tel" class="addRess" />
				</li>
				<li class="pad1rem setup-btm flexs-bw">
					<select id="Province" class="address-sel">
						<option value="0">请选择省</option>
					</select>
					<i class="iconfont icon-gengduo"></i>
				</li>
				<li class="pad1rem setup-btm flexs-bw">
					<select id="City" class="address-sel">
						<option value="0">请选择市</option>
					</select>
					<i class="iconfont icon-gengduo"></i>
				</li>
				<li class="pad1rem setup-btm flexs-bw">
					<select id="County" class="address-sel">
						<option value="0">请选择县</option>
					</select>
					<i class="iconfont icon-gengduo"></i>
				</li>
				<li class="address-edit address-inp">
					<input class="detail-adres addRess w100" type="text" placeholder="请填写详细地址，不少于5个字" />
				</li>
			</ul>
			<div class="setup-u top1rem address-inp flexs-bw">
				<span> 设为默认地址 </span>
				<input class="mui-before" type="checkbox" />
			</div>
		</section>
		<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script src="//cdn.bootcss.com/layer/3.0.1/mobile/layer.js"></script>
		<script src="../../src/js/all.js" type="text/javascript" charset="utf-8"></script>
		<script>
			var addvalue = silks.getByVal("typeID");
				var saves = document.getElementById("saves");
				if(!addvalue) {
					$(".header-txt").html("添加新地址");
					$.get(urls + "/BaseSelect/GetProvince", function(res) {
						var opt = '<option value="0">请选择省</option>';
						if(res.Return_ID == 0) {
							for(var i = 0; i < res.Return_Data.length; i++) {
								opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
							}
						}
						$("#Province").html(opt);
					});
				} else {
					$.get(urls + "/WorkMembersAddress/GetMembersAddressByID", {
						AddressID: addvalue,
						MembersID: MembersID,
						AppID: AppID,
						Token: Token
					}, function(res) {
						console.log(res)
						if(res.Return_ID == 0) {
							var province = res.Return_Data.Province_ID;
							var city = res.Return_Data.City_ID;
							var country = res.Return_Data.County_ID;
							$(".addRess:eq(0)").val(res.Return_Data.MembersAddress_Receiver);
							$(".addRess:eq(1)").val(res.Return_Data.MembersAddress_Phone);
							$(".addRess:eq(2)").val(res.Return_Data.MembersAddress_Address);
							if(res.Return_Data.MembersAddress_Mark == 1){
							 	var marks = document.querySelector("input[type=checkbox]");
							 	marks.checked = true;
							 	marks.className =  "mui-after";
							}
							$.get(urls + "/BaseSelect/GetProvince", function(res) {
								var opt = '<option value="0">请选择省</option>';
								if(res.Return_ID == 0) {
									for(var i = 0; i < res.Return_Data.length; i++) {
										opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
									}
								}
								$("#Province").html(opt);
								$("#Province").val(province);
							});

							$.get(urls + "/BaseSelect/GetCity", {
								ProvinceID: res.Return_Data.Province_ID
							}, function(res) {
								var opt = '<option value="0">请选择市</option>';
								if(res.Return_ID == 0) {
									for(var i = 0; i < res.Return_Data.length; i++) {
										opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
									}
								}
								$("#City").html(opt);
								$("#City").val(city);

							});
							$.get(urls + "/BaseSelect/GetCounty", {
								CityID: res.Return_Data.City_ID
							}, function(res) {
								var opt = '<option value="0">请选择县</option>';
								if(res.Return_ID == 0) {
									for(var i = 0; i < res.Return_Data.length; i++) {
										opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
									}
								}
								$("#County").html(opt);
								$("#County").val(country);
							});

						}
					})
				}
				saves.addEventListener("touchstart", function() {
					if($(".addRess:eq(0)").val() == "") {
						layers("收货人不能为空");
					} else if(/^1\d{10}$/.test($(".addRess:eq(1)").val()) != true) {
						layers("手机号码输入有误");
					} else if($("#Province").val() == "0") {
						layers("请选择省");
					} else if($("#City").val() == "0") {
						layers("请选择市");
					} else if($("#County").val() == "0") {
						layers("请选择县");
					} else if($(".addRess:eq(2)").val().length < 5) {
						layers("请填写详细地址，不少于5个字");
					} else {
						var datas = {
							Mode: !addvalue ? "new" : "edit",
							AddressID: !addvalue ? "" : addvalue,
							MembersID: MembersID,
							Receiver: $(".addRess:eq(0)").val(),
							Phone: $(".addRess:eq(1)").val(),
							ProvinceID: $("#Province").val(),
							CityID: $("#City").val(),
							CountyID: $("#County").val(),
							Address: $(".addRess:eq(2)").val(),
							Mark: $("input[type=checkbox]").is(":checked") ? 1 : 0,
							AppID: AppID,
							Token: Token
						};
						$.post(urls + "/WorkMembersAddress/MembersAddressEdit", datas, function(res) {
							if(res.Return_ID == 0) {
								layers(res.Return_Mess);
							} else {
								layers(res.Return_Mess);
							}
						});
					}

				})

				$(document).on("change", "#Province", function() {
					var ProvinceID = $(this).find("option:selected").val();
					$.get(urls + "/BaseSelect/GetCity", {
						ProvinceID: ProvinceID
					}, function(res) {
						var opt = '<option value="0">请选择市</option>';
						if(res.Return_ID == 0) {
							for(var i = 0; i < res.Return_Data.length; i++) {
								opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
							}
						}
						$("#City").html(opt);
						$("#County").html('<option value="0">请选择县</option>');
					});
				});

				$(document).on("change", "#City", function() {
					var CityID = $(this).find("option:selected").val();
					$.get(urls + "/BaseSelect/GetCounty", {
						CityID: CityID
					}, function(res) {
						var opt = '<option value="0">请选择县</option>';
						if(res.Return_ID == 0) {
							for(var i = 0; i < res.Return_Data.length; i++) {
								opt += '<option value=' + res.Return_Data[i].Value + '>' + res.Return_Data[i].Text + '</option>'
							}
						}
						$("#County").html(opt);
					});
				});
				
				$("input[type=checkbox]").click(function () {
					
					if(this.checked){
						 this.className = "mui-after";
					}else{
						this.className = "mui-before"; 
					}
				});

		</script> 
	</body>

</html>